package com.xinsmartsky.penmaji.penmaji;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.xinsmartsky.penmaji.adapter.LocalFileAdapter;
import com.xinsmartsky.penmaji.common.FileUtil;
import com.xinsmartsky.penmaji.myview.MyButtion;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Stack;

/**
 * Created by wangxiao on 16-3-25.
 * 文件管理器
 */
public class FileMangerActivity extends BaseActivity implements View.OnClickListener{
    private ListView listView;

    private ImageButton returnBtn;

    private TextView titleView;
    private int check = 0;// 创建文件的标志

    private LocalFileAdapter adapter;

    private Map<String, String> map = null;//这个用来保存复制和粘贴的路径

    private File root;

    private List<File> list;
    private View view;
    private LinearLayout layout;
    private MyButtion addButtion, searchButtion, menuButtion;
    // path的堆栈
    private static Stack<String> pathStack;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // requestWindowFeature(Window.FEATURE_NO_TITLE);
        view = LayoutInflater.from(FileMangerActivity.this).inflate(
                R.layout.local_file_dialog, null);
        setContentView(view);
        String storageState = Environment.getExternalStorageState();

        if (storageState.equals(Environment.MEDIA_MOUNTED)) {
            root = new File(Environment.getExternalStorageDirectory()
                    .getAbsolutePath());
        } else {
            finish();
        }

        initView();
    }

    /**
     133          * 初始化视图
     134          */
    private void initView() {

        layout = (LinearLayout) findViewById(R.id.local_File_lin);

        listView = (ListView) findViewById(R.id.local_File_drawer);

        adapter = new LocalFileAdapter(this, list);

        listView.setAdapter(adapter);

        map = new HashMap<String, String>();

        listView.setOnItemClickListener(new DrawerItemClickListener());
        registerForContextMenu(listView);//注册一个上下文菜单

        returnBtn = (ImageButton) findViewById(R.id.local_File_return_btn);

        returnBtn.setOnClickListener(this);

        titleView = (TextView) findViewById(R.id.local_File_title);
        addButtion = (MyButtion) findViewById(R.id.myButtion1);
        addButtion.setOnClickListener(this);
//        searchButtion.setOnClickListener(this);
//        menuButtion.setOnClickListener(this);
        searchData(root.getAbsolutePath());

        addPath(root.getAbsolutePath());

    }

    /**
     333          * 添加路径到堆栈
     334          *
     335          * @param path
     336          */

    public void addPath(String path) {

        if (pathStack == null) {
            pathStack = new Stack<String>();
        }

        pathStack.add(path);
    }

    private class DrawerItemClickListener implements
            ListView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                                long id) {

            adapter.setSelectedPosition(position);

            selectItem(position);
        }
    }

    /**
     86          * 查询调用方法
     87          */

    public void searchData(String path) {
        searchViewData(path);
        titleView.setText(path);
    }

    private void selectItem(int position) {

        String filePath = adapter.getFiles().get(position).getAbsolutePath();

        String fileName = adapter.getFiles().get(position).getName();

        if (adapter.getFiles().get(position).isDirectory()) {
            searchData(filePath);
            addPath(filePath);
        } else if (adapter.getFiles().get(position).isFile()) {
            // 判断是否为文件
            FileUtil.openFile(FileMangerActivity.this, fileName, adapter.getFiles()
                    .get(position));
        }

    }

    /**
     94          * 查询view的数据
     95          */
    public void searchViewData(final String path) {

        putAsyncTask(new AsyncTask<Void, Void, Boolean>() {

            @Override
            protected Boolean doInBackground(Void... params) {

                try {
                    list = FileUtil.getFileListByPath(path);
                } catch (Exception e) {
                    return false;
                }

                return true;
            }

            @Override
            protected void onPostExecute(Boolean result) {
                super.onPostExecute(result);

                if (result) {

                    adapter.setFiles(list);

                    adapter.setSelectedPosition(-1);

                    adapter.notifyDataSetChanged();

                } else {
                    Toast.makeText(FileMangerActivity.this, "查询失败", Toast.LENGTH_LONG)
                            .show();
                }
            }
        });
    }



    @Override
    public void onClick(View v) {

    }
}
